package com.dongjiantong.farmrecords.model;

/**
 * 防疫中心-诊疗记录
 */

import com.dongjiantong.common.utils.JsonMapper;
import com.dongjiantong.mybatis.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import lombok.Data;

import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Table(name = "farmrecords_diagnosis")
@Data
public class FarmRecordsDiagnosis extends BaseEntity implements Serializable {
    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();
    private static final long serialVersionUID = 660989643290012985L;

    /**
     *  单据编号
     */
    private String number;

    /**
     * 诊疗期次 (牛档案)
     */
    private Integer times;

    /**
     * 关联牛档案id
     */
    private Long cow_record_id;

    /**
     * 牛号
     */
    private String cow_number;

    /**
     * 养殖场id
     */
    private Long farm_id;

    /**
     * 养殖场名称
     */
    private String farm_name;

    /**
     * 省id
     */
    private Integer province_id;

    /**
     * 市id
     */
    private Integer city_id;

    /**
     * 县id
     */
    private Integer region_id;

    /**
     * 镇id
     */
    private Integer street_id;

    /**
     * 村id
     */
    private Integer village_id;

    /**
     * 诊疗人员
     */
    private String user_name;

    /**
     * 诊疗日期
     */
    private Date date;

    /**
     * 养殖场区域
     */
    private Integer area;

    /**
     * 栏舍id
     */
    private Long stable_id;

    /**
     * 栏舍号
     */
    private String stable_number;

    /**
     * 圈舍号
     */
    private String barn_number;

    /**
     * 动物种类
     */
    private Integer breed_second_type;

    /**
     * 病因
     */
    private String illness_reason;

    /**
     * 单位
     */
    private Integer unit;

    /**
     * 发病数
     */
    private Integer cases;

    /**
     * 用药方法
     */
    private Integer drug_used_method;

    /**
     * 治疗效果：1、康复（头数） 2、死亡（头数）
     */
    @JsonIgnore
    private String result;
    @Transient
    private Map<String,Object> result_toObject;

    /**
     * 禽兽耳标号
     */
    @JsonIgnore
    private String ear_tag_number_json;

    @Transient
    private List<String> ear_tag_number;

    /**
     * 用药名称
     */
    private String drug_name;

    /**
     * 诊疗剂量
     */
    private Integer drug_used_quantity;

    /**
     * 休药期
     */
    private String withdrawal_period;

    /**
     * 停药日期
     */
    private Date withdrawal_date;

    /**
     * 备注
     */
    private String remark;

    /**
     * 状态，0:未提交，1：生效
     */
    private Integer status;

    /**
     * 单据状态 1：正常，-1：删除
     */
    private Integer delete_flag;

    /**
     *
     */
    private Date created_at;

    /**
     *
     */
    private Date updated_at;

    /**
     * 关联的文件表图片list
     */
    @Transient
    private List picList;

    /**
     * 关联的文件表视频list
     */
    @Transient
    private List videoList;

    public void setEar_tag_number_json(String ear_tag_number_json) {
        this.ear_tag_number_json = ear_tag_number_json;

        if (Strings.isNullOrEmpty(ear_tag_number_json)) {
            this.ear_tag_number = null;
        } else {
            try {
                this.ear_tag_number = objectMapper.readValue(ear_tag_number_json,new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setEar_tag_number (List<String> ear_tag_number) {
        this.ear_tag_number = ear_tag_number;

        if (ear_tag_number == null || ear_tag_number.isEmpty()) {
            this.ear_tag_number_json = "[]";
        } else {
            try {
                this.ear_tag_number_json = objectMapper.writeValueAsString(ear_tag_number);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    public void setResult(String result) {
        this.result = result;

        if (Strings.isNullOrEmpty(result)) {
            this.result_toObject = null;
        } else {
            try {
                this.result_toObject = objectMapper.readValue(result, new TypeReference<Map<String,Object>>() {});
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setResult_toObject(Map<String,Object> result_toObject) {
        this.result_toObject = result_toObject;

        if (result_toObject == null || result_toObject.isEmpty()) {
            this.result = null;
        } else {
            try {
                this.result = objectMapper.writeValueAsString(result_toObject);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}